<?php
	/**
	*Author 雷日锦 362606856@qq.com 
	*控制器自动生成
	*/
	if(!defined("ROOT_PATH")) exit("die Access ");
	class yuanxiControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$where=" siteid=".SITEID." AND status=2 AND pid=0";
			$url="/admin.php?m=yuanxi&a=default";
			$limit=120;
			$start=get("per_page","i");
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=M("yuanxi")->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$v['child']=M("yuanxi")->select(array(
						"where"=>" pid=".$v['id']." AND status=2 "
					));
					$data[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->goassign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url
				)
			);
			$this->smarty->display("yuanxi/index.html");
		}
		
		public function onAdd(){
			$id=get_post("id","i");
			if($id){
				$data=M("yuanxi")->selectRow(array("where"=>"id={$id}"));			
			}
			$plist=M("yuanxi")->select(array(
				"where"=>" siteid=".SITEID." AND pid=0 AND status=2 "
			));
			$this->smarty->goassign(array(
				"data"=>$data,
				"plist"=>$plist
			));
			$this->smarty->display("yuanxi/add.html");
		}
		
		public function onSave(){
			$id=get_post("id","i");
			$data=M("yuanxi")->postData();
			$data['siteid']=SITEID;
			$data['status']=2;
			if($id){
				M("yuanxi")->update($data,"id='$id'");
			}else{
				$tits=post('tits','h');
				$data['dateline']=time();
				$ts=explode("\r\n",$tits);
				if(!empty($ts)){
					foreach($ts as $t){
						if(empty($t)) continue;
						$data['title']=sql($t);
						M("yuanxi")->insert($data);
					}
					
				}
				
				
			}
			$this->goall("保存成功");
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			$row=M("yuanxi")->selectRow("id=".$id);
			if($row['pid']==0){
				$child=M("yuanxi")->selectRow("pid=".$id);
				if($child){
					$this->goAll("下级分类存在，不可删除",1);
				}
			}
			M("yuanxi")->update(array("status"=>99),"id=$id");
			$this->goall("删除成功",0);
		}
		
		
	}

?>